﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Drawing;
using Pdm.Commons;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting;

namespace Pdm.Wpf
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public SortedList<double, List<PdmOptimization>> displayList;
        public int firstOut { get; set; }
        public int coefficientCnt { get; set; }
        public List<Specification> specs { get; set; }

        public Window1()
        {
            InitializeComponent();
            loadData();
            loadMatrix();
        }
        public void loadMatrix()
        {
            matrix.buildEmptyMatrix(displayList[0][0].dmuList.coefficients.GetRange(firstOut, coefficientCnt));
        }
        public void loadData()
        {
            displayList = DataProvider.loadOptimizations();

            firstOut = displayList[0][0].dmuList.getIDofFirstOutputCoeffiecient();
            coefficientCnt = displayList[0][0].dmuList.coefficients.Count - 1;


            //set up scrollbar
            scrollbar.Maximum = displayList.Count - 1;
            scrollbar.Minimum = 0;
            scrollbar.LargeChange = 1;
            scrollbar.SmallChange = 1;
            scrollbar.Value = 0;

            //set up product list
            productItems.DataContext = displayList.Values[Int32.Parse(scrollbar.Value.ToString())];
            productItems.ItemsSource = displayList.Values[Int32.Parse(scrollbar.Value.ToString())];


            mainChart.ChartAreas.Add("main");
            mainChart.Legends.Add("main");
            //mainChart.Legends["main"].DockedToChartArea = "main";
            //mainChart.Legends["main"].Alignment = StringAlignment.Near;
            //mainChart.Legends["main"].IsDockedInsideChartArea = false;
            //mainChart.ChartAreas["main"].Area3DStyle.Enable3D = true;

            //set up chart area


            specs = new List<Specification>();
            foreach (Specification spec in displayList[0][0].dmuList.coefficients)
            {
                specs.Add(spec);
            }

            /*mainChart.Series.Add("DWP");            
            mainChart.Series["DWP"].Points.DataBindXY(specs.GetRange(firstOut, coefficientCnt),
                    "name", displayList[0][0].urmWeights.GetRange(firstOut, coefficientCnt), "value");
            mainChart.Series["DWP"].ChartType = SeriesChartType.Radar;
            mainChart.Series["DWP"]["RadarDrawingStyle"] = "Line";
            mainChart.Series["DWP"]["AreaDrawingStyle"] = "Polygon";
            mainChart.Series["DWP"]["CircularLabelsStyle"] = "Horizontal";*/

            foreach (Dmu dmu in displayList[0][0].dmuList)
            {
                mainChart.Series.Add(dmu.name);
                mainChart.Series[dmu.name].Points.DataBindXY(specs.GetRange(firstOut, coefficientCnt),
                    "name",
                    displayList.Values[Int32.Parse(scrollbar.Value.ToString())][dmu.id].pdmWeights.GetRange(firstOut, coefficientCnt),
                    "value");

                mainChart.Series[dmu.name].ChartType = SeriesChartType.Radar;
                mainChart.Series[dmu.name]["RadarDrawingStyle"] = "Line";
                mainChart.Series[dmu.name]["AreaDrawingStyle"] = "Polygon";
                mainChart.Series[dmu.name]["CircularLabelsStyle"] = "Horizontal";

            }

            //TODO: Set to initial maximum of series
            mainChart.ChartAreas["main"].AxisY.Enabled = AxisEnabled.False;//.Interval = 0.1;
            mainChart.ChartAreas["main"].AxisY.Maximum = 0.3;

            /*foreach (Dmu dmu in displayList[0][0].dmuList)
            {
                ColumnSeries x = new ColumnSeries();
                x.Name = dmu.name;
                x.Title = dmu.name;
                x.ItemsSource = displayList.Values[Int32.Parse(scrollbar.Value.ToString())][dmu.id].pdmWeights.GetRange(firstOut, coefficientCnt);
                x.IndependentValueBinding = new Binding("spec.name");
                x.DependentValueBinding = new Binding("value");
                x.IndependentAxis = ax;
                //MainChart.Series.Add(x);
            }*/
        }

        private void scrollbar_scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {
            int newVal = Int32.Parse(Math.Truncate((double)e.NewValue).ToString());
            productItems.DataContext = displayList.Values[newVal];
            productItems.ItemsSource = displayList.Values[newVal];
            scrollbar.Value = newVal;

            //update chart
            foreach (Dmu dmu in displayList[0][0].dmuList)
            {
                //ColumnSeries x = (ColumnSeries)MainChart.Series[dmu.id];
                //x.ItemsSource = displayList.Values[newVal][dmu.id].pdmWeights.GetRange(firstOut, coefficientCnt); ;
                System.Windows.Forms.DataVisualization.Charting.Series x = mainChart.Series[dmu.name];

                x.Points.DataBindXY(specs.GetRange(firstOut, coefficientCnt),
                    "name", displayList.Values[Int32.Parse(scrollbar.Value.ToString())][dmu.id].pdmWeights.GetRange(firstOut, coefficientCnt),
                    "value");
            }
            //MainChart.Refresh();
        }

        /* public void testChart()
         {
             // Populate series data
             double[] yValues = { 65.62, 75.54, 60.45, 34.73, 85.42, 55.9, 63.6, 55.2, 77.1 };
             string[] xValues = { "France", "Canada", "Germany", "USA", "Italy", "Spain", "Russia", "Sweden", "Japan" };

             mainChart.Series.Add("Default");
             mainChart.ChartAreas.Add("Default");

             // Set radar chart type
             mainChart.Series["Default"].ChartType = SeriesChartType.Radar;

             // Set radar chart style (Area, Line or Marker)
             mainChart.Series["Default"]["RadarDrawingStyle"] = "Area";

             // Set circular area drawing style (Circle or Polygon)
             mainChart.Series["Default"]["AreaDrawingStyle"] = "Polygon";

             // Set labels style (Auto, Horizontal, Circular or Radial)
             mainChart.Series["Default"]["CircularLabelsStyle"] = "Horizontal";

             // Show as 3D
             //mainChart.ChartAreas["Default"].Area3DStyle.Enable3D = true;
         }*/
        /*private static DmuList getTestList()
        {
            DmuList dmus = new DmuList();
            dmus.Add(new Dmu("Prod1"));
            dmus.Add(new Dmu("Prod2"));
            dmus.Add(new Dmu("Prod3"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Input, "InputDummy"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out1"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out2"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out3"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out4"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out5"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out6"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out7"));
            dmus.coefficients.Add(new Specification(Pdm.Commons.Type.Output, "Out8"));

            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[0], 1));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[1], 1.52380));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[2], 0.94117));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[3], 1.90909));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[4], 1.53846));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[5], 1.46666));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[6], 1.40909));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[7], 1.90625));
            dmus[0].coefficients.Add(new SpecificationInstance(dmus.coefficients[8], 1.61904));

            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[0], 1));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[1], 1.76190));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[2], 1.41176));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[3], 1.27272));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[4], 1.46153));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[5], 1.73333));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[6], 1.72727));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[7], 1.96875));
            dmus[1].coefficients.Add(new SpecificationInstance(dmus.coefficients[8], 2.04761));

            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[0], 1));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[1], 1.61904));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[2], 0.97058));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[3], 1.45454));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[4], 1.00000));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[5], 1.06666));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[6], 1.72727));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[7], 2.03125));
            dmus[2].coefficients.Add(new SpecificationInstance(dmus.coefficients[8], 1.76190));

            return dmus;

            /*PdmOptimization pdm = new PdmOptimization(dmus, 0);
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[0], 11));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[1], 21));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[2], 34));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[3], 11));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[4], 13));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[5], 15));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[6], 22));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[7], 32));
            pdm.urmWeights.Add(new SpecificationInstance(pdm.dmuList.coefficients[8], 21));
        }*/
    }
}
